﻿/**************************************************************

Copyright 2012, Lars Warholm, Norway (lars@witservices.no)

This file is part of NBEncode, a .NET library for encoding and decoding
"bencoded" data

NBEncode is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

NBEncode is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with NBEncode.  If not, see <http://www.gnu.org/licenses/>.

**************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Web;
using Base32;
using OSS.NBEncode.Extensions;
using OSS.NBEncode.Transforms;
using OSS.NBEncode.Entities;

namespace OSS.SampleApp
{
    static class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 1)
            {
                var inFileStream = new FileStream(args[0], FileMode.Open);
	            string link ="magnet:?xt=urn:btih:a707a808de5c15de68b0e05f3b4a4c885317a5c7&dn=%D0%94%D0%B5%D1%82%D1%81%D0%BA%D0%B0%D1%8F+%D0%B4%D0%B8%D1%81%D0%BA%D0%BE%D1%82%D0%B5%D0%BA%D0%B0+%D0%BE%D1%82+%D1%81%D0%BC%D0%B5%D1%88%D0%B0%D1%80%D0%B8%D0%BA%D0%BE%D0%B2";
	            link = HttpUtility.UrlDecode(link);

				
                var transform = new BObjectTransform();
				BDictionary bObject = transform.DecodeNext(inFileStream) as BDictionary;
				IBObject info = bObject.FindSection("info");
	            Encoding encoding = GetEncoding(bObject);
				string fileName = ((info as BDictionary).FindSection("name") as BByteString).ConvertToText(encoding);
				byte[] infoData = info.ToByteArray();
				string hashString = Sha1Encode(infoData);
				string magnetLink = string.Format("magnet:?xt=urn:btih:{0}&db={1}", hashString, fileName);
				Console.WriteLine(magnetLink);

				//TextOutput textOut = new TextOutput(4);
				//textOut.WriteObject(0, bObject);

            }
            else
            {
                Console.WriteLine("\nSyntax is:\nOSS.SampleApp.exe <torrent file path>");
            }
        }

	    private static string Sha1Encode(byte[] bytes)
	    {
		    using (SHA1 hasher = new SHA1Managed())
		    {
				return Base32Encoder.Encode(hasher.ComputeHash(bytes));
		    }
	    }

		private static Encoding GetEncoding(BDictionary torrent)
		{
			BByteString encoding = torrent.FindSection("encoding") as BByteString;
			return encoding == null ? Encoding.ASCII : Encoding.GetEncoding(encoding.ConvertToText(Encoding.ASCII));
		}

		//public static IBObject FindSection(this BDictionary dictionary, string sectionName)
		//{
		//    if (dictionary == null)
		//        throw new ArgumentNullException();

		//    foreach (KeyValuePair<BByteString, IBObject> keyValuePair in dictionary.Value)
		//    {
		//        string key = keyValuePair.Key.ConvertToText(Encoding.ASCII);
		//        if (key == sectionName)
		//            return keyValuePair.Value;
		//    }

		//    return null;
		//}

		//public static byte[] ToByteArray(this IBObject obj)
		//{
		//    using (MemoryStream MS = new MemoryStream())
		//    {
		//        var transform = new BObjectTransform();
		//        transform.EncodeObject(obj, MS);
		//        MS.Position = 0;
		//        return MS.ToArray();
		//    }
		//}
    }
}
